package com.hsource.doctorcenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.hsource.common.utils.CommonResp;
import com.hsource.common.utils.DateUtil;
import com.hsource.common.utils.bean.BeanUtils;
import com.hsource.common.utils.resultUtils.controller.BaseController;
import com.hsource.doctorcenter.domain.clinic.Clinic;
import com.hsource.doctorcenter.domain.clinic.ClinicApi;
import com.hsource.doctorcenter.domain.clinic.ClinicHttpApi;
import com.hsource.doctorcenter.service.api.IApiService;
import com.hsource.doctorcenter.service.clinic.IClinicService;
import com.hsource.doctorcenter.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/9/22 15:17
 * @describe CLINIC-CONTROLLER
 */
@Controller
@RequestMapping("/clinic")
public class IClinicController extends BaseController {

    @Autowired
    private IClinicService clinicService;
    @Autowired
    private IApiService apiService;

    /**
     * 获取图文急诊|快捷电话科室
     * @param userId
     * @return
     */
    @PostMapping("/getImageTextAndPhoneClinics")
    @ResponseBody
    public CommonResp getImageTextAndPhoneClinics(String userId,String clinicType) throws ParseException, NoSuchAlgorithmException {
        String imageTextCllinics = apiService.getImageTextAndPhoneClinics(userId,clinicType);
        JSONObject jsonObject = JSONObject.parseObject(imageTextCllinics);
        if(jsonObject.get("error").toString().equals("0")){
            List<ClinicHttpApi> clinics = JSONObject.parseArray(jsonObject.get("clinic_info").toString(), ClinicHttpApi.class);
            List<Clinic> clinic = clinicService.selectClinicList(new Clinic());
            if(clinicType.equals("imagetext")) {
                //过滤是否有医生坐诊
                clinics = clinics.stream().filter(c -> StringUtils.isNull(c.getDisabled())).collect(Collectors.toList());
                //过滤时间
                long nowTimeLog = Calendar.getInstance().getTime().getTime();
                clinics = clinics.stream().filter(c -> nowTimeLog - DateUtil.getDateFromStr(c.getBegin_time(), "").getTime() > 0
                        && nowTimeLog - DateUtil.getDateFromStr(c.getEnd_time(), "").getTime() < 0).collect(Collectors.toList());
                //查询就诊价格
                clinics.parallelStream().forEach(c->{
                    c.setPrice(clinic.stream().filter(cl->cl.getClinicNo().equals(c.getClinic_no())).findFirst().get().getImageTextPrice());
                });

            }
            if(clinicType.equals("phone")) {
                //查询就诊价格
                clinics.parallelStream().forEach(c->{
                    c.setDuration("10分钟");
                    c.setBegin("9:00");
                    c.setEnd("21:00");
                    c.setPrice(clinic.stream().filter(cl->cl.getClinicNo().equals(c.getClinic_no())).findFirst().get().getPhonePrice());
                });
            }
            Map<String,Object> map=new HashMap<>();
            map.put("clinics",clinics);
            return success(map);
        }else{
            return failed();
        }
    }

    /**
     * 获取科室数据
     * @return
     */
    @PostMapping("/getClinics")
    @ResponseBody
    public CommonResp getClinics(){
        Clinic clinic=new Clinic();
        clinic.setParent("0");
        List<Clinic> clinics = clinicService.selectClinicList(clinic);
        List<ClinicApi> clinicApis=new ArrayList<>();
        clinics.stream().forEach(a->{
            ClinicApi clinicApi=new ClinicApi();
            BeanUtils.copyBeanProp(clinicApi,a);
            clinicApis.add(clinicApi);
        });
        clinicApis.sort((a,b)->Integer.parseInt(a.getClinicNo())-Integer.parseInt(b.getClinicNo()));
        Map<String,Object> clinicsMap=new HashMap<>();
        clinicsMap.put("clinics",clinicApis);
        return success(clinicsMap);
    }
}
